<?php

namespace App\Api\Dao;

use App\Exceptions\DaoException;
use App\Helpers\ResponseEnum;
use App\Models\TongTransferMoney;
use App\Models\TongTransferMoneyLog;
use App\Models\TongUser;
use App\Models\TongUserProfit;
use Illuminate\Support\Facades\DB;

class TransferMoneyDao extends BaseDao
{

    function getBuildQuery($data)
    {
        // TODO: Implement getBuildQuery() method.
        $query = TongTransferMoney::query();
        if(checkIsHasField($data,'id')){
            $query->where('id',$data['id']);
        }
        if(checkIsHasField($data,'u_id')){
            $query->where('u_id',$data['u_id']);
        }
        if(checkIsHasField($data,'to_id')){
            $query->where('to_id',$data['to_id']);
        }
        return $query;
    }

    /**
     * 用户发起转账
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|true
     */
    public function to_user_money(array $data)
    {
        $to_query = TongUser::query()->where('mobile',$data['to_mobile']);
        $to_info = $to_query->first();
        $insert_data = [
            'money'=>$data['money'],
            'u_id'=>$data['u_id'],
            'to_id'=>$to_query->exists()?$to_info['id']:0, //转账的接受人
            'status'=>1,
            'desc'=>'转账成功'
        ];
        DB::beginTransaction();
        try {
            //修改此次转账的状态
            //TongTransferMoney::query()->where('u_id',$data['u_id'])->where('id',$data['id'])->update(['status'=>1]);
            //增加接收人的金额
            TongUser::query()->where('id',$to_info['id'])->increment('money',$data['money']);
            //$to_user_info = TongUser::query()->where('id',$to_info['id'])->first();
            TongUserProfit::query()->create([
                'u_id'=>$to_info['id'],
                'money'=>+$data['money'],
                'type'=>12,//好友转账
                'symbol'=>1,
                'status'=>1,
                'remark'=>'好友发起的转账接受',
                'befor_money'=>$to_info['money'],
                'after_money'=>bcadd($to_info['money'],$data['money'],2),
            ]);
            $userInfo = TongUser::query()->where('id',$data['u_id'])->first();
            //减少转账人的金额
            TongUser::query()->where('id',$data['u_id'])->decrement('money',$data['money']);

            TongUserProfit::query()->create([
                'u_id'=>$data['u_id'],
                'money'=>-$data['money'],
                'type'=>12,//好友转账
                'symbol'=>2,
                'status'=>1,
                'remark'=>'好友发起的转账扣除',
                'befor_money'=>$userInfo['money'],
                'after_money'=>bcsub($userInfo['money'],$data['money'],2),
            ]);
            //判断用户的钱包余额
            $result = TongTransferMoney::query()->create($insert_data);
            DB::commit();
            return $result;
        }catch (\Exception $exception){
            DB::rollBack();
            throw new DaoException(ResponseEnum::HTTP_ERROR,$exception->getMessage());
        }

    }

    /**
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getToUserMoneyInfo(array $data)
    {
        return $this->getBuildQuery($data)->with(['toUserInfo'])->first();
    }

    /**
     * @param array $data
     * @return void
     */
    public function confirmUserMoney(array $data)
    {
        //获取用户的详情
        $user_info  = TongUser::query()->where('id',$data['u_id'])->first();
        //获取转账的详情
        $transfer_info = TongTransferMoney::query()->where('u_id',$data['u_idf'])->where('id',$data['id'])->first();

        DB::transaction(function () use($data,$transfer_info) {
            //修改此次转账的状态
            TongTransferMoney::query()->where('u_id',$data['u_id'])->where('id',$data['id'])->update(['status'=>1]);
            //增加接收人的金额
            TongUser::query()->where('id',$transfer_info['to_id'])->increment('money',$transfer_info['money']);
            //减少转账人的金额
            TongUser::query()->where('id',$transfer_info['u_id'])->decrement('money',$transfer_info['money']);
            //记录转账的记录
            TongTransferMoneyLog::query()->create([
                'u_id'=>$transfer_info['u_id'],
                'to_id'=>$transfer_info['to_id'],
                'money'=>$transfer_info['money']
            ]);
        });

    }
}
